<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: kingdom.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../';
$templates_path = '../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Set custom language path
$user->set_custom_lang_path($phpbb_root_path.'games/aod/language/');

//load our language file
$user->add_lang('common');

// Now add the phpBB language path
$user->set_custom_lang_path($phpbb_root_path . 'language');

// Load the Game Console language file
$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}

if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

// Default Vars
$error_msgs = null;
$pagination_url = append_sid("kingdom.$phpEx");
$admin = 0;

// Var Requests
$kingdom_page = TRUE; // Highlights the kingdom tab
$cmd = request_var('cmd','');
$nid = request_var('nid', 0);
$start = request_var('start', 0);

// AOD Config
$reports_per_page = $aod_config['reports_per_page'];


// Gather some user info
$user_id = $user->data['user_id'];
$account_status = check_account_status($user_id);
if ($account_status == FALSE)
{
	redirect(append_sid("{$phpbb_root_path}/games/index.$phpEx"));
}

// Fetch kingdom data
$king = fetch_kingdom_data($user_id);
$admin_access = $king['admin_access'];
$kingdom_sponsor = $king['kingdom_sponsor'];
$kingdom_level = $king['kingdom_level'];
$kingdom_status = $king['kingdom_status'];

if($kingdom_sponsor)
{
	$sponsor_status = sprintf($user->lang['SPONSOR_TICKS_REMAINING'],$kingdom_sponsor);
}else{
	$sponsor_status = $user->lang['NONE'];
}

// Check to see if this user is a admin staff member
if ($user_id == 2 || $admin_access == 1)
{
	$admin = 1;
}

// Check kingdom status to see if this account is frozen
if($kingdom_status == 1)
{
	// Lets make sure the main admin can't be blocked
	if ($user_id <> 2){
		trigger_error($user->lang['FROZEN'] . back_link());
	}
}

// Fetch the amount of each unit that this kingdom has
$unitcount = fetch_kingdom_unit_counts($user_id);

// Build unit list	
$sql = "SELECT *
FROM " . AOD_UNITS_TABLE . "
WHERE unit_level <= $kingdom_level
AND unit_level > 0
ORDER BY id ASC";
$result = $db->sql_query($sql);
			
while ($row = $db->sql_fetchrow($result))
{

	$uid = $row['id'];
	$unit_name = $row['unit_name'];
	$unit_off = $row['unit_off'];
	$unit_def = $row['unit_def'];
		
	$template->assign_block_vars('unitsrow', array(
		'NAME'		=> $unit_name,
		'OFF'		=> number_format($unit_off),
		'DEF'		=> number_format($unit_def),
		'OWN'		=> number_format($unitcount[$uid]),		
		)
	);	
}	
$db->sql_freeresult($result);
	

// Fetch the amount of each building that this kingdom has
$buildingcount = fetch_kingdom_building_counts($user_id);

// Fetch the current building level of each building that this kingdom has
$buildinglvl = fetch_kingdom_building_levels($user_id);

// Build unit list	
$sql = "SELECT *
FROM " . AOD_BUILDINGS_TABLE . "	
ORDER BY id ASC";
$result = $db->sql_query($sql);
			
while ($row = $db->sql_fetchrow($result))
{
	$bid = $row['id'];
	$building_name = $row['building_name'];
	$building_net = $row['building_net'];
	$building_def = $row['building_def'];
	$building_lvl = $buildinglvl[$bid];
	
	$template->assign_block_vars('buildingrow', array(
		'NAME'		=> $building_name,
		'LVL'		=> number_format($building_lvl),
		'DEF'		=> number_format($building_def),
		'OWN'		=> number_format($buildingcount[$bid]),
		)
	);	
}	
$db->sql_freeresult($result);


// Build Quest List
$total_reports = count_kingdom_news($user_id);

$sql = "SELECT *
FROM " . AOD_KINGDOM_NEWS_TABLE . " 
WHERE user_id = $user_id 
ORDER BY id DESC";
$result = $db->sql_query_limit($sql, $reports_per_page, $start);
			
while ($row = $db->sql_fetchrow($result))
{
	$date = $row['date'];
	$report = $row['report'];

	$template->assign_block_vars('newsrow', array(
		'DATE'		=>	$date,
		'REPORT'	=>	$report,
		)
	);	
}	
$db->sql_freeresult($result);

// Display an error messages to the user
if($error_msgs){
	$error_msgs = '<span class="errormsg"><b>'.$error_msgs.'</b></span>';
}

$template->assign_vars(array(
	'DRAGON_COINS'					=> number_format($king['kingdom_coins']),
	'VILLAGERS'						=> number_format($king['kingdom_villagers']),
	'SOLDIERS'						=> number_format($king['kingdom_soldiers']),
	'EXP_TO_NEXT_LVL'				=> number_format(exp_to_next_level($kingdom_level)),
	'WINS'							=> number_format($king['kingdom_wins']),
	'LOSSES'						=> number_format($king['kingdom_losses']),
	'OFF_BONUS'						=> number_format($king['kingdom_off_bonus'] * 100).'%',
	'DEF_BONUS'						=> number_format($king['kingdom_def_bonus'] * 100).'%',
	'UNIT_BONUS'					=> number_format($king['kingdom_unit_bonus'] * 100).'%',
	'SPONSOR_STATUS'				=> $sponsor_status,
	'FARMS_BASE_INCOME'				=> number_format($king['kingdom_farms_income']),
	'FARMS_TOTAL_INCOME'			=> number_format(($king['kingdom_farms_income'] * $king['kingdom_farms']) * $king['kingdom_farms_lvl']),
	'TIMBER_MILLS_BASE_INCOME'		=> number_format($king['kingdom_timber_mills_income']),
	'TIMBER_MILLS_TOTAL_INCOME'		=> number_format(($king['kingdom_timber_mills_income'] * $king['kingdom_timber_mills']) * $king['kingdom_timber_mills_lvl']),
	'IRON_MINES_BASE_INCOME'		=> number_format($king['kingdom_iron_mines_income']),
	'IRON_MINES_TOTAL_INCOME'		=> number_format(($king['kingdom_iron_mines_income'] * $king['kingdom_iron_mines']) * $king['kingdom_iron_mines_lvl']),
	'GOLD_MINES_BASE_INCOME'		=> number_format($king['kingdom_gold_mines_income']),
	'GOLD_MINES_TOTAL_INCOME'		=> number_format(($king['kingdom_gold_mines_income'] * $king['kingdom_gold_mines']) * $king['kingdom_gold_mines_lvl']),
	'STAMINA_INCOME'				=> number_format($king['kingdom_stamina_income']),
	'TURNS_INCOME'					=> number_format($king['kingdom_turns_income']),
	'VILLAGERS_INCOME'				=> number_format($king['kingdom_villagers_income']),
	
	'MAX_FOOD_STORAGE'				=> number_format(($king['kingdom_granaries'] * $king['kingdom_granaries_max']) * $king['kingdom_granaries_lvl']),
	'MAX_WOOD_STORAGE'				=> number_format(($king['kingdom_woodsheds'] * $king['kingdom_woodsheds_max']) * $king['kingdom_woodsheds_lvl']),
	'MAX_IRON_STORAGE'				=> number_format(($king['kingdom_ironpits'] * $king['kingdom_ironpits_max']) * $king['kingdom_ironpits_lvl']),
	'MAX_GOLD_STORAGE'				=> number_format(($king['kingdom_vaults'] * $king['kingdom_vaults_max']) * $king['kingdom_vaults_lvl']),

	
	'PAGINATION'			=>	'<span class="stext">'.generate_pagination($pagination_url, $total_reports, $reports_per_page, $start).'</span>',
	'PAGE_NUMBER'			=>	on_page($total_reports, $reports_per_page, $start),

	)
);

include($phpbb_root_path . 'games/aod/includes/page_header.' . $phpEx);


// Output page
page_header($game_name.' - '.$user->lang['KINGDOM']);

$template->set_filenames(array(
	'body' => $templates_path.'kingdom_body.html')
);

page_footer();
?>